<?php

class Auth extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('Password_hash');
        $this->load->model('account_model');
    }

    public function index()
    {

        if (Identity::getInstance()->isLoggedIn()) {

            redirect('zdl');
            die();
        }

        $data = array(
            'error' => ''
        );

        if ($this->input->post('user')) {

            $user = trim($this->input->post('user'));
            $pass = trim($this->input->post('pass'));

            $userLogin = $this->account_model->getLoginInfo($user);
            if (!empty($userLogin)) {

                if ($this->password_hash->CheckPassword($pass, $userLogin['pwd'])) {

                    Identity::getInstance()->authenticate($userLogin['id'], $userLogin['user'], $userLogin['role'], 0);
                    redirect('zdl');
                } else {

                    $data['error'] = 'Sorry, Password not valid';
                }
            } else {
                $data['error'] = 'Sorry, Account ID not exists';
            }
        }

        $languageMap = array(
            'EN' => array(
                'user' => 'A.Account',
                'pass' => 'A.Password'
            ),
            'VND' => array(
                'user' => 'số tài khoản',
                'pass' => 'mật mã'
            )
        );

        $lang = trim($this->input->get('language', TRUE));

        if (!$lang || !isset($languageMap[$lang]))
            $lang = 'EN';

        $data['lang'] = $languageMap[$lang];

        $this->load->view('auth/login_view', $data);
    }

    public function signout()
    {
        if (!Identity::getInstance()->isLoggedIn()) {
            redirect();
            die();
        }

        Identity::getInstance()->signOut();
        redirect();
    }

    public function users()
    {
        if (Identity::getInstance()->getRole() != '-1') redirect('zdl');

        $this->load->view('auth/index_users_view', array());
    }

    public function userManage()
    {
        if (Identity::getInstance()->getRole() != '-1') redirect('zdl');

        $data = array();

        $data['allUsers'] = $this->account_model->getAllUsers();

        $this->load->view('auth/user_manage', $data);
    }

    public function rm($id)
    {
        if (Identity::getInstance()->getRole() != '-1') redirect('zdl');

        $this->account_model->removeUser($id);

        redirect('auth/userManage');
    }

    public function edit()
    {
        if (Identity::getInstance()->getRole() != '-1') {
            echo 'error';
            die();
        }

        $val = $this->input->post('ns', TRUE);
        $name = $this->input->post('name', TRUE);
        $id = $this->input->post('id', TRUE);

        if ($val != '' && $name != '' && $id != '') {

            $arrEdit = array();

            if ($name == 'pwd') {

                $arrEdit[$name] = $this->password_hash->HashPassword($val);
                $val = 'Click to edit';
            } elseif ($name == 'role') {

                $arrEdit[$name] = ($val == '-1') ? -1 : 1;
                $val = ($val == '-1') ? 'Admin' : 'User';
            } else {
                $arrEdit[$name] = $val;
            }

            if ($this->account_model->update($arrEdit, array('id' => $id))) {

                echo $val;
                die();
            }
        }
        echo 'error';
    }

    public function add()
    {
        if (Identity::getInstance()->getRole() != '-1') redirect('zdl');

        $user = $this->input->post('user', TRUE);
        $pwd = $this->input->post('pwd', TRUE);
        $role = $this->input->post('role', TRUE);

        $role = ($role == '-1') ? -1 : 1;
        $pwd = $this->password_hash->HashPassword($pwd);

        $this->account_model->addUser(array(
            'user' => $user,
            'pwd' => $pwd,
            'role' => $role
        ));

        redirect('auth/userManage');
    }

}
